//
//  MTNewClubDetailSwitchCell.swift
//  MT
//
//  Created by ooops on 2020/5/26.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import Eureka
import MTCategory
import GrowingTextView
import RxSwift
import RxCocoa

public class MTNewClubDetailSwitchCell: Cell<Bool>, CellType {
    
    /// 显示模式
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "名称"
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    lazy var switchButton: UISwitch = {
        let s = UISwitch()
        s.isOn = false
        s.addTarget(self, action: #selector(switchOnOff(sender:)), for: .touchUpInside)
        return s
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    public var tipString: String? = nil
    
    private var isHiddenLineView: Bool = true {
        didSet {
            lineView.isHidden = isHiddenLineView
        }
    }
    
    @objc private func switchOnOff(sender: UISwitch) {
        //self.delegate?.didOpenSwitch(sender: sender, data: self.data)
        self.row.value = sender.isOn
    }
    
    open override func cellCanBecomeFirstResponder() -> Bool {
        return false
    }

    open override func cellBecomeFirstResponder(withDirection direction: Direction) -> Bool {
        return false
    }

    open override func cellResignFirstResponder() -> Bool {
        return false
    }
    
    
    
    override public func setup() {
        super.setup()
        self.backgroundColor = UIColor.theme.background
        self.selectionStyle = .none
        self.textLabel?.isHidden = true
        self.detailTextLabel?.isHidden = true
        self.height = { UITableView.automaticDimension }
        setupUI()
        makeConstraint()
        if row.value != nil {
            self.switchButton.isOn = row.value!
        }
    }
    
    
    override public func update() {
        super.update()
        tipLabel.text = tipString
        if row.value != nil {
            self.switchButton.isOn = row.value!
        }
    }
}

// MARK: - 布局相关
extension MTNewClubDetailSwitchCell {
    
    private func setupUI() {
        contentView.addSubview(tipLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(switchButton)
    }
    
    private func makeConstraint() {
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        
        switchButton.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
            make.size.equalTo(CGSize(width: 51, height: 31))
        }
    }
}

// The custom Row also has the cell: CustomCell and its correspond value
public final class MTNewClubDetailSwitchCellRow: Row<MTNewClubDetailSwitchCell>, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
        cellProvider = CellProvider<MTNewClubDetailSwitchCell>.init()
    }
}



